<?php namespace Summer\Hjd\Classes;

use Illuminate\Support\Carbon;
use Summer\Hjd\Models\BonusRecordModel;
use Summer\Hjd\Models\BonusTypeModel;
use Summer\Hjd\Models\ChangeModel;
use Summer\Hjd\Models\DetailedBonusModel;
use Summer\Hjd\Models\DetailedCashModel;
use Summer\Hjd\Models\DetailedHeartModel;
use Summer\Hjd\Models\DetailedLineModel;
use Summer\Hjd\Models\DetailedScoreModel;
use Summer\Hjd\Models\DetailedStockModel;
use Summer\Hjd\Models\FinanceModel;
use Summer\Hjd\Models\WalletModel;

class CommonApi
{
    /**
     * 处理奖金
     * @param $user_id 获奖用户
     * @param $relate_user_id 相关用户
     * @param $money 奖金金额
     * @param $bonus_type_slug 奖金类型
     */
    public function settleBonus($user_id,$relate_user_id,$money,$bonus_type_slug){
        $bonus_type=BonusTypeModel::where('slug',$bonus_type_slug)->where('status',1)->first();
        if(!$bonus_type){
            return false;
        }
        //分配方案
        $allot=$bonus_type->allot;
        if(!$allot){
            return false;
        }
        $fee_ratio=$bonus_type->fee_ratio;
        $fee=$money*$fee_ratio*0.01;
        //奖金记录
        $bonus_record_model=new BonusRecordModel();
        $bonus_record_model->user_id=$user_id;
        $bonus_record_model->relate_user_id=$relate_user_id;
        $bonus_record_model->bonus_type_slug=$bonus_type_slug;
        $bonus_record_model->bonus=$money;
        $bonus_record_model->bonus_real=$money-$fee;
        $bonus_record_model->fee=$fee;
        $bonus_record_model->allot=$allot;
        $bonus_record_model->status=1;
        $bonus_record_model->save();
        //流水记录
        foreach ($allot as $av){
            $this->settleDetailed($user_id,$relate_user_id,$money*$av['allot_ratio']*0.01,1,$av['allot_currency'],[
                'change_slug'=>$bonus_type->change_slug,
                'table_id'=>$bonus_record_model->id,
                'table_name'=>'bonus_record',
            ]);
        }
    }

    /**
     * 增加流水记录
     * @param $user_id 流水用户
     * @param $relate_user_id 相关用户
     * @param $money 流水金额,不带符号
     * @param $inout 增加或减少 1增加 0减少
     * @param $currency_slug 币种slug
     * @param array $params 其他参数,其中change_slug-流水类型是必须的
     */
    public function settleDetailed($user_id,$relate_user_id,$money,$inout,$currency_slug,array $params){
        //操作余额
        $wallet=WalletModel::where('user_id',$user_id)->where('currency_slug',$currency_slug)->first();
        $money_before=$wallet->balance;
        if($inout==1){
            $money_after=$wallet->balance+$money;
        }else{
            $money_after=$wallet->balance-$money;
        }
        $wallet->balance=$money_after;
        $wallet->save();
        //流水信息
        $change=ChangeModel::where('slug',$params['change_slug'])->first();
        if($change->inc_exp>0){
            //增加财务统计
            $today_date=Carbon::today();
            $finance=FinanceModel::where('today_at',$today_date)->first();
            if($finance){
                if($change->inc_exp==1){//收入
                    FinanceModel::where('today_at',$today_date)->increment('income',$money);
                }
                if($change->inc_exp==2){//支出
                    FinanceModel::where('today_at',$today_date)->increment('expense',$money);
                }
            }else{
                $finance_data['status']=1;
                $finance_data['today_at']=$today_date;
                if($change->inc_exp==1){//收入
                    $finance_data['income']=$money;
                }
                if($change->inc_exp==2){//支出
                    $finance_data['expense']=$money;
                }
                FinanceModel::create($finance_data);
            }
        }
        //币种信息,多此一步，为了不以id做筛选
        switch ($currency_slug){
            case 'bonus':
                $detailed_model=new DetailedBonusModel();
                break;
            case 'cash':
                $detailed_model=new DetailedCashModel();
                break;
            case 'score':
                $detailed_model=new DetailedScoreModel();
                break;
            case 'stock':
                $detailed_model=new DetailedStockModel();
                break;
            case 'heart':
                $detailed_model=new DetailedHeartModel();
                break;
            case 'line':
                $detailed_model=new DetailedLineModel();
                break;
        }
        $detailed_model->user_id=$user_id;
        $detailed_model->relate_user_id=$relate_user_id;
        $detailed_model->money=$money;
        $detailed_model->inout=$inout;
        $detailed_model->money_before=$money_before;
        $detailed_model->money_after=$money_after;
        $detailed_model->change_slug=$params['change_slug'];
        $detailed_model->comment=isset($params['comment'])?$params['comment']:$change->title;
        $detailed_model->table_id=$params['table_id']??0;
        $detailed_model->table_name=$params['table_name']??'';
        $detailed_model->status=1;
        $detailed_model->save();
        return $detailed_model->id;
    }

}